<?php
/**
 * Created by PhpStorm.
 * User: lx
 * Date: 2020/1/13
 * Time: 16:05
 */
namespace app\api\controller;

use app\api\model\MtPoster;
use app\api\model\MtShopHouse;
use app\api\model\MtShopHouseTypeitem;
use app\util\ReturnCode;

class HomePage extends Base
{
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 通过城市查询房屋
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function seachByCity()
    {
        $cityID = intval($this->request->get("cityID"));
        if (!$cityID) {
            return $this->error(ReturnCode::EMPTY_PARAMS, "缺少参数cityID");
        }
        $seachRes = (new MtShopHouse())->where([["area", "like", $cityID], ["status", "=", "1"]])->select();
        if (!$seachRes) {
            return $this->error(ReturnCode::DB_READ_ERROR, "查询失败");
        }
        return $this->success("查询成功", $seachRes);
    }

    /**
     * 房间属性列表
     * @param $p
     * @param $limit
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function houseTypeList($p, $limit)
    {
        $p = intval($this->request->get("p")) ? intval($this->request->get("p")) : 1;
        $limit = intval($this->request->get("p")) ? intval($this->request->get("p")) : 5;
        $typeList = (new MtShopHouseTypeitem())->where("status", "1")->page($p, $limit)->select();
        if (!$typeList) {
            return $this->error(ReturnCode::DB_READ_ERROR, "查询失败");
        }
        return $this->success("查询成功", $typeList);
    }

    /**
     * 通过房间属性查询
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function seachByItem()
    {
        $houseType = intval($this->request->get("typeItem"));
        if (!$houseType) {
            return $this->error(ReturnCode::EMPTY_PARAMS, "缺少参数typeItem");
        }
        $limit = intval($this->request->get("limit")) ? intval($this->request->get("limit")) : config("apiadmin.ADMIN_LIST_DEFAULT");
        $posterList = (new MtShopHouse())->where([["houseTypeItem", "like", $houseType], ["status", "=", "1"]])->limit($limit)->order("reserveNum desc")->select();
        if (!$posterList) {
            return $this->error(ReturnCode::DB_READ_ERROR, $posterList);
        }
        return $this->success("查询成功", $posterList);
    }

    /**
     * 根据入住时间查询
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function seachByTime(){
        $start = $this->request->get("start") ? $this->request->get("start") : date("md");
        $end = $this->request->get("end") ? $this->request->get("end") : date("md");
        $p = $this->request->get("p") ? $this->request->get("p") : 1;
        $limit = $this->request->get("limit") ? $this->request->get("limit") : config("apiadmin.ADMIN_LIST_DEFAULT");
        $cityID = $this->request->get("cityID"); // @todo  可能自动定位城市 传过来就不是cityID了
        $houseList = (new MtShopHouse())->where([["area","like",$cityID],["status","=","1"]])->field("houseID")->page($p,$limit)->select();
        $db = ""; // @todo
        $liveList = (new $db)->where([["id","in",$houseList],["cTime","not in",[strtotime($start),strtotime($end)]]])->select(); // @todo
        if(!$liveList){
            return $this->error(ReturnCode::DB_READ_ERROR,"查询失败");
        }
        return $this->success("查询成功",$liveList);
    }
    /**
     * 广告获取
     * @param int $type 广告类型 (1:banner)
     * @param int $limit
     * @return array|\PDOStatement|string|\think\Collection|void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getPoster()
    {
        $type = intval($this->request->get("type")) ? intval($this->request->get("type")) : 1;
        $limit = intval($this->request->get("limit")) ? intval($this->request->get("limit")) : 2;
        $posterList = (new MtPoster())->where([["type", "=", $type], ["status", "=", "1"]])->limit($limit)->order("ctime desc")->select();
        if (!$posterList) {
            return $this->error(ReturnCode::DB_READ_ERROR, $posterList);
        }
        return $this->success("查询成功", $posterList);
    }
}